package com.sentinel.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sentinel.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    @SentinelResource(value = "sayHi",blockHandler = "sayHiError")
    public String sayHi(String name) throws InterruptedException {
        String result = helloService.sayHi(name);
        //int i = 1/10; //此异常不会通过下面的BlockException来处理
        return result;
    }
    // 资源被规则限制后,切面灰抛出异常,切面catch逻辑需要使用blockHandler的方法处理
    public String sayHiError(String name,BlockException ex){
        String result = "sorry, i am" + name;
        return result;
    }
}
